using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;


namespace ControlPoint.ControlAPI {
    public class Main {
        private Dictionary<string, DeviceController> _controllers;
        //private SerializableDictionary<string, string> _eventScripts;
        private Configuration _configuration;


        ZWaveDimmer d;

        public Dictionary<string, Device> Devices {
            get {
                return this._configuration._devices;
            }
        }
        public Dictionary<string, DeviceController> Controllers {
            get {
                return this._configuration._controllers;
            }
        }

        private ControlAPI.SettingsList _settings;
        private EezeeScript.ScriptManager _scriptManager;

        public Main() {
            this._configuration = new Configuration();
            this.LoadScripting();
            this._configuration.LoadEvents();
            this._configuration.LoadConfiguration();
            foreach (Device d in this._configuration._devices.Values) {
                if (d is IAction) {
					Console.WriteLine("Implements");
                    ((IAction)d).RegisterAction(this.HandleAction);
                }
            }
            //this._configuration.SaveEvents();
            //this._configuration.SaveConfiguration();

        }

        public void LoadScripting() {
            _scriptManager = new EezeeScript.ScriptManager();
            _scriptManager.Loader = new ControlPoint.ControlAPI.ScriptLoaderMemoryString();
            EezeeScript.CommandPrototype commandPrototype = null;

            commandPrototype = new EezeeScript.CommandPrototype("Wait");
            commandPrototype.AddParameterType(typeof(int));
            _scriptManager.RegisterCommand(commandPrototype);

            commandPrototype = new EezeeScript.CommandPrototype("SetValue");
            commandPrototype.AddParameterType(typeof(String));
            commandPrototype.AddParameterType(typeof(int));
            _scriptManager.RegisterCommand(commandPrototype);

            commandPrototype = new EezeeScript.CommandPrototype("GetValue");
            commandPrototype.AddParameterType(typeof(String));
            _scriptManager.RegisterCommand(commandPrototype);

        }

        public void ZW() {

            this._configuration.Events.Add("d:" + "Press", @"
SET count TO 0
SET level TO 0
WHILE count < 5
    ADD 30 TO level
    ADD 1 TO count
    SetValue ""zw:21815248:2:38:0"" level
    Wait 3000
ENDWHILE");

            ZWaveController c = new ZWaveController("COM4");
            TimerController tc = new TimerController();

            _configuration._controllers.Add(c.ID, c);
            _configuration._controllers.Add(tc.ID, tc);
            /*
            TimerButton t = new TimerButton(tc, 23, 21);
            _configuration._devices.Add(t.ID, t);
            t.RegisterAction(HandleAction);
            */
            //d = new ZWaveDimmer(c, (byte)0x02, 0);
            //_configuration._devices.Add(d.ID, d);
            /*
            ZWaveSensor s = new ZWaveSensor(c, (byte)0x03, 1);
            _devices.Add(s.ID, s);
            ZWaveMotion m = new ZWaveMotion(c, (byte)0x03, 0);
            _devices.Add(m.ID, m);
            */
            
            this._configuration.SaveEvents();
            this._configuration.SaveConfiguration();

            //Console.WriteLine("sID" + d.ID);
            Console.WriteLine("Online");
            Console.ReadLine();

            
            Console.WriteLine("Pressing Enter will turn light off");
            Console.ReadLine();
            d.Off();

            Console.WriteLine("Pressing Enter will exit");
            Console.ReadLine();

            c = null;
            Console.ReadLine();
            Console.Clear();
            Environment.Exit(-1);
        }


        public void HandleAction(IAction device, string action, ActionValue value) {
            string scriptString;

            if (this._configuration.Events.TryGetValue(device.ID + ":" + action, out scriptString)) {

				Console.WriteLine("ActionHandled: '" + device.ID + ":" + action + "'" );
				
                ScriptEngine se;
                EezeeScript.Script script = null;

                //GetValue ""zw:21815248:3:49:1""
                script = new EezeeScript.Script(_scriptManager, scriptString);
                se = new ScriptEngine(script, this);
                se.ScriptContext.Execute();
			} else {
				Console.WriteLine("Not ActionHandled: '" + device.ID + ":" + action + "'" );
            }
        }

        public void LoadDevices() {
            throw new NotImplementedException();
        }

        public void Start() {
            throw new NotImplementedException();
        }
    }
}
